package com.qmds.common.minio.utils;

import com.qmds.common.minio.config.MinioProperties;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * --- 代码敲烂 月薪过万 ---
 *
 * @author ShiJiaWei
 * @date 2024/8/22
 * @desc: minio工具类
 */
@Component
public class MinioUtils {

    @Value("${minio.bucketName}")
    private String bucketName;

    @Resource
    private MinioProperties minioProperties;

    @Autowired
    private MinioClient minioClient;

    /**
     * 判断bucket是否存在，不存在则创建
     */
    public void existBucket(String name) {
        try {
            boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(name).build());
            if (!exists) {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(name).build());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    public String upload(MultipartFile file, String fileName) {

        // 如果桶不存在，直接创建
        existBucket(bucketName);

        InputStream in = null;
        try {
            in = file.getInputStream();
            // 上传到minio
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName) //.bucket("img/" + bucketName)
                    .object("img/" + fileName)
                    .stream(in, in.available(), -1)
                    .contentType(file.getContentType())
                    .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 返回上传的文件的路径
        return String.format("%s/%s/%s/%s",
                minioProperties.getEndpoint(),
                minioProperties.getBucketName(),
                "img",
                fileName);
    }


    public String uploadError(MultipartFile file, String fileName) {

        // 如果桶不存在，直接创建
        existBucket(bucketName);

        InputStream in = null;
        try {
            in = file.getInputStream();
            // 上传到minio
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName) //.bucket("img/" + bucketName)
                    .object("error/" + fileName)
                    .stream(in, in.available(), -1)
                    .contentType(file.getContentType())
                    .build()
            );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 返回上传的文件的路径
        return String.format("%s/%s/%s/%s",
                minioProperties.getEndpoint(),
                minioProperties.getBucketName(),
                "img",
                fileName);
    }


}
